//
//  YBClassDetailViewController.m
//  ZhongWangYuanApp
//
//  Created by apple on 18/6/3.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YBClassDetailViewController.h"
#import "YBObjectClassDetailListTableViewCell.h"
#define classCell @"YBObjectClassDetailListTableViewCell"
#import "YBObjectIntroductionModel.h"
#import "ZJVidoViewController.h"

@interface YBClassDetailViewController ()<UITableViewDelegate , UITableViewDataSource>
/**    */
@property (strong, nonatomic) UITableView  *tableView;
@property (nonatomic , strong) NSMutableArray * dataArray;
@property (nonatomic , assign) NSInteger page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

@property (nonatomic , assign) BOOL isEnterNext;

@property (nonatomic , assign) BOOL isSignIn;
@end

@implementation YBClassDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.isEnterNext = NO;
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"currentIsSign"]) {
        ((YBObjectIntroductionModel *)self.model).issignin = @"1";
    }
    if (((YBObjectIntroductionModel *)self.model).issignin) {
        self.isSignIn = YES;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(option_objectClassLearnerIsSignIn) name:@"ObjectClassLearnerIsSignIn" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(option_objectClassProsessIsSignIn) name:@"ObjectClassProsessIsSignIn" object:nil];
    // Do any additional setup after loading the view.
    [self layoutUI];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 0;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
        
    }];
    [self.tableView.mj_header beginRefreshing];
}


-(void)option_objectClassLearnerIsSignIn
{
    self.isSignIn = YES;
}


-(void)option_objectClassProsessIsSignIn
{
    self.isSignIn = YES;
}


#pragma mark - 界面
- (void)layoutUI{
    
    UIView * topBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 5)];
    topBackView.backgroundColor = [UIColor themeGrayColor];
    self.tableView.tableHeaderView = topBackView;
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.view , 0)
    .bottomSpaceToView(self.view , 0);
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.isEnterNext) {
        [self synchronousClassData];
        self.isEnterNext = NO;
    }
}

-(void)synchronousClassData{
    [YBRequestManager request_synchronousClassDataWithIdString:((YBObjectIntroductionModel *)self.model).ID successBlock:^(id object) {
        [self.tableView.mj_header beginRefreshing];
    } failBlock:^(id object) {
        [self.tableView.mj_header beginRefreshing];
    }];
}


- (void)netWorking_refresh{
    self.page = 0;
    __weak typeof(self) weakSelf = self;
    [YBRequestManager request_objectClassWithIdString:((YBObjectIntroductionModel *)self.model).ID pageindex:[NSString stringWithFormat:@"%ld" , self.page] successBlock:^(id object) {
        NSString *result = [object objectForKey:@"code"];
        if ([result integerValue] == 1) {
            if ([object[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray *array = [object objectForKey:@"data"];
                if (array.count == 0) {
                    //                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                    
                }else{
                    [weakSelf.dataArray removeAllObjects];
                    weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array] mutableCopy];
                    [weakSelf.tableView reloadData];
                }
            }
            
        }else{
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        
    } failBlock:^(id object) {
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
    }];
    
    
}

- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    [YBRequestManager request_objectClassWithIdString:((YBObjectIntroductionModel *)self.model).ID pageindex:[NSString stringWithFormat:@"%ld" , self.page] successBlock:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSString *result = [object objectForKey:@"code"];
        if ([result integerValue] == 1) {
            if ([object[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray *array = [object objectForKey:@"data"];
                if (array.count == 0) {
                    //                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                    
                }else{
                    weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array] mutableCopy];
                    [weakSelf.tableView reloadData];
                }
                
            }
        }else{
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
    }];
    
    
}


- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    [YBRequestManager request_objectClassWithIdString:((YBObjectIntroductionModel *)self.model).ID pageindex:[NSString stringWithFormat:@"%ld" , self.page] successBlock:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
        NSString *result = [object objectForKey:@"code"];
        if ([result integerValue] == 1) {
            NSArray *array = [object objectForKey:@"data"];
            if (array.count == 0) {
                //                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                weakSelf.page --;
                
            }else{
                [weakSelf.dataArray addObjectsFromArray:[[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array] mutableCopy]];
                [weakSelf.tableView reloadData];
            }
            
        }else{
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
    }];
    
    
}


#pragma mark - 私有方法


#pragma mark - 监听事件



#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 125 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YBObjectClassDetailListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:classCell forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;

    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //判断是否报名
    if (self.isSignIn) {
        //已经报名的操作，跳转
        self.isEnterNext = YES;
        ZJVidoViewController * zjVideoVC = [[ZJVidoViewController alloc]init];
        zjVideoVC.model = self.dataArray[indexPath.row];
        zjVideoVC.classId = ((YBObjectIntroductionModel *)self.model).ID;
        [self.navigationController pushViewController:zjVideoVC animated:YES];
    }
    else{
        //没有报名自动报名
        [YJProgressHUD show:@"报名中..." inView:[UIApplication sharedApplication].keyWindow mode:YJProgressModeLoading];
        [YBRequestManager request_objectSignInWithIdString:((YBObjectIntroductionModel *)self.model).ID successBlock:^(id object) {
            [YJProgressHUD hide];
            if ([object[@"code"] integerValue] == 1) {
                if ([object[@"data"][@"result"] integerValue] == 1) {
                    [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"currentIsSign"];
                    self.isSignIn = YES;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"ObjectClassDetailIsSignIn" object:nil];
                    self.isEnterNext = YES;
                    //跳转
                    ZJVidoViewController * zjVideoVC = [[ZJVidoViewController alloc]init];
                    zjVideoVC.model = self.dataArray[indexPath.row];
                    zjVideoVC.classId = ((YBObjectIntroductionModel *)self.model).ID;
                    [self.navigationController pushViewController:zjVideoVC animated:YES];
                }
                else{
                    [YJProgressHUD showMsgWithoutView:[NSString stringWithFormat:@"%@",object[@"data"][@"message"]]];
                }
            }
            else{
                [YJProgressHUD showMsgWithoutView:@"报名失败"];
            }
        } failBlock:^(id object) {
            [YJProgressHUD showMsgWithoutView:@"报名失败"];
        }];
    }
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 5, self.view.bounds.size.width, [UIScreen zj_screenHeight] - LP_NavViewHeight - 50 * [UIScreen zj_screenHScale] - ItemHeight * 465 - 5 ) style:UITableViewStylePlain];
        [_tableView adaptiveIOS11];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        [_tableView registerClass:[YBObjectClassDetailListTableViewCell class] forCellReuseIdentifier:classCell];
        
        
    }
    return _tableView;
}


- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
